const express = require("express");
const app = express();
const router = express.Router();

const { mysql,executeQuery } = require("../../mysql");

// 创建学生
router.post("/create-student", async (req, res) => {
  try {
    // 接收并验证数据
    const { id, name, sex, grade, class: classe, major_ID, major_Name, department_ID, department_Name, tel , created_at } = req.body;    
    // 构建参数化查询语句
    const insertStudentSql = `INSERT INTO tb_Student (id, password, student_name, student_sex, grade, classe, major_id, major, Department_ID, Department_Name, Student_Tel , created_at) VALUES (?, 123456, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
    // 执行查询
    const result = await executeQuery(insertStudentSql, [id, name, sex, grade, classe, major_ID, major_Name, department_ID, department_Name, tel , created_at]);
    
    if(result){
        // 返回成功响应
      res.json({
        success: true,
        id: id,
      });
    }

  } catch (error) {
    // 处理错误
    console.error('Error executing query:', error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

module.exports = router;
